import axios from 'axios'
import {getServerUrl, defaultParams} from './config'

let ULogin = '/user/login'          // 用户登陆
let CheckU = '/user/checkusername'    // 检查用户名
let UOut   = '/user/logout'            // 用户退出
let CS     = '/user/checkstatus'       // 检查用户登陆状态
let GUI    = '/user/getuserinfo'       // 获取用户信息

export default {
  login(params) {
    return __(ULogin, params)
  },
  checkUserName(params) {
    return __(CheckU, params)
  },
  logout(params) {
    return __(UOut, params)
  },
  checkStatus() {
    return __(CS)
  },
  getUserInfo(params) {
    return _(GUI, params)
  }
}

function _(url, p) {
  const u = getServerUrl(url)

  const d = Object.assign(defaultParams, p)

  return axios.get(u, {
    params: d
  }).then((r) => {
    return Promise.resolve(r)
  })
}

function __(url, p) {
  const u = getServerUrl(url)

  const d = Object.assign(defaultParams, p)

  return axios.post(u, d).then(function (res) {
    console.log(res)
    return Promise.resolve(res.data)
  })
}

